package com.meirengu.webview.controller;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.webview.service.KwStatisticInterfaceService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ${DESCRIPTION}
 *
 * @author Mao
 * @create 2017-12-19 12:27
 */
@Controller
@RequestMapping("/kwStatisticInterfaceController")
public class StaticInterfaceController extends BaseController {


    @Autowired
    private KwStatisticInterfaceService kwStatisticInterfaceService;

    Logger logger = LoggerFactory.getLogger(StaticInterfaceController.class);


    @RequestMapping("statisInterface")
    @ResponseBody
    public Result statisInterface() {
        try {
            kwStatisticInterfaceService.statisInterface();
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            String param = "";
            logger.error(">>StaticInterfaceController.statisInterface throw exception: {} happens. params:{}.", ExceptionUtils.getFullStackTrace(e), param);
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }

    }

    @RequestMapping("statisInterfaceByCode")
    @ResponseBody
    public Result statisInterfaceByCode(@RequestParam(value = "dataDate", required = true) String dataDateBegin,
                                        @RequestParam(value = "interfaceCode", required = true) String interfaceCode) {
        try {
            kwStatisticInterfaceService.statisInterfaceByCode(dataDateBegin, interfaceCode);
            return super.setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
        } catch (Exception e) {
            e.printStackTrace();
            String param = "";
            logger.error(">>StaticInterfaceController.statisInterface throw exception: {} happens. params:{}.", ExceptionUtils.getFullStackTrace(e), param);
            return super.setResult(StatusCode.INTERNAL_SERVER_ERROR, null, StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        }

    }


    /**
     * 手工统计数据
     *
     * @param dataDateBegin
     * @param dataDateEnd
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/statisticByMonth")
    public Result handXbb3(@RequestParam(value = "dataDateBegin", required = true) String dataDateBegin,
                           @RequestParam(value = "dataDateEnd", required = true) String dataDateEnd) throws Exception {
        if (StringUtils.isBlank(dataDateBegin) || StringUtils.isBlank(dataDateEnd)) {
            return setResult(StatusCode.MISSING_ARGUMENT, null, StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        kwStatisticInterfaceService.statistics(dataDateBegin, dataDateEnd);
        return setResult(StatusCode.OK, null, StatusCode.codeMsgMap.get(StatusCode.OK));
    }


}


